package com.hnkywl.sts.service.sys;

import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.sys.GwpzDao;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.sys.Gw;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Melody_for_love
 * Date: 13-7-15
 * Time: 下午3:19
 * To change this template use File | Settings | File Templates.
 */
@Service
public class GwpzService {
    private GwpzDao gwpzDao;
    private GsjgDao gsjgDao;
    private GsjgService gsjgService;

    @Transactional
    public String findAllGsjgGw(Long gsjgId, int pageNo, int pageSize) {
        Page<Gw> gsjgGwPage = this.gwpzDao.findGsjgGwList(gsjgId, pageNo, pageSize);
        List<Gw> gsjgGwList = gsjgGwPage.getResult() == null ? new ArrayList<Gw>() : gsjgGwPage.getResult();
        Map<String,Object> otherParam = new HashMap<String,Object>();
        otherParam.put("list_jsList","mc");
        String gwJson = BaseUtils.fetchCollectionToJson(gsjgGwList,otherParam, "id","mc","jsList","ms");
        return "{\"total\":" + gsjgGwPage.getTotalCount() + ",\"rows\":[" + gwJson + "]}";
    }

    @Transactional
    public String findAllGsjgGwSearch( int pageNo, int pageSize) {
        Page<Gw> gwPage = this.gwpzDao.findGwSearchList(pageNo, pageSize);
        List<Gw> gwList = gwPage.getResult() == null ? new ArrayList<Gw>() : gwPage.getResult();
        Map<String,Object> otherParam = new HashMap<String,Object>();
        otherParam.put("list_jsList","mc");
        String gwJson = BaseUtils.fetchCollectionToJson(gwList,otherParam, "id","mc","jsList", "rzyq","ms","pxh");
        return "{\"total\":" + gwPage.getTotalCount() + ",\"rows\":[" + gwJson + "]}";
    }

     //添加岗位配置
    @Transactional
    public void saveGwpz(Gsjg gsjg,Long gsjgId) throws RuntimeException {
        Gsjg clone = gsjgService.findGsjgDetail(gsjgId);
        for(int i = 0;i < clone.getGsjggwList().size();i++){
            for( int j = 0;j<gsjg.getGsjggwList().size();j++){
                if(Long.parseLong(clone.getGsjggwList().get(i).getId().toString())==Long.parseLong(gsjg.getGsjggwList().get(j).getId().toString())){
                    gsjg.getGsjggwList().remove(gsjg.getGsjggwList().get(j));
                }
            }
        }
        for(int k=0;k<gsjg.getGsjggwList().size();k++){
            clone.getGsjggwList().add(gsjg.getGsjggwList().get(k));
        }
        this.gsjgDao.save(clone);
    }

    //删除岗位配置
    @Transactional
    public void deleteGwpz(Long gwId,Long gsjgId) {
        Gw gw=gwpzDao.get(gwId);
        gw.setLastUpdated(new Date());
        Gsjg clone = gsjgService.findGsjgDetail(gsjgId);
        List<Gw> gwList =clone.getGsjggwList();
        for(int i = 0 , len= gwList.size();i<len;++i){
            if(gwList.get(i).getId()==gwId){
                gwList.remove(i);
                --len;//减少一个
                --i;
            }
        }
        clone.setGsjggwList(gwList);
        this.gsjgDao.save(clone);
    }

    @Autowired
    public void setGwpzDao(GwpzDao gwpzDao) {
        this.gwpzDao = gwpzDao;
    }
    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
